<?php


namespace app\store\model\dealer;
use app\common\model\DistributionSetting as DistributionSettingModel;
use think\Session;

class DistributionSetting extends DistributionSettingModel{


    /*
     * 添加分销方案一配置
     */
    public function add($data,$dealer_type,$dealerCase){

        if (!isset($data['distribution_type']) || empty($data['distribution_type'])) {
            $this->error = '请选择分销类型';
            return false;
        }

        $setting_content = json_encode($data,256);

        $wxapp_id = Session::get('yoshop_store')['wxapp']['wxapp_id'];

        db('wxapp')->where(['wxapp_id'=>$wxapp_id])->update(['dealer_case'=>$dealerCase]);

        return $this->allowField(true)
            ->insertGetId([
                'wxapp_id'=>$wxapp_id,
                'setting_content'=>$setting_content,
                'create_time'=>time(),
                'update_time'=>time(),
                'dealer_type'=>$dealer_type
            ]);
    }
    /*
     * 修改分销方案一配置
     */
    public function edit($id,$data,$dealerCase){

        if (!isset($data['distribution_type']) || empty($data['distribution_type'])) {
            $this->error = '请选择分销类型';
            return false;
        }

        $wxapp_id = Session::get('yoshop_store')['wxapp']['wxapp_id'];

        $setting_content = json_encode($data,256);

        db('wxapp')->where(['wxapp_id'=>$wxapp_id])->update(['dealer_case'=>$dealerCase]);

        return $this->allowField(true)->where(['id'=>$id])->update([
            'wxapp_id'=>$wxapp_id,
            'setting_content'=>$setting_content,
            'update_time'=>time(),
        ]);

    }



    /*
    * 添加分销方案二配置
    */
    public function addtow($data,$dealer_type){

        $newData = [
            'common' => $data['common'],
            'staff' => $data['staff'],
            'wgd' => $data['wgd'],
            'vip' => $data['vip'],
        ];
        $setting_content = json_encode($newData,256);

        $wxapp_id = Session::get('yoshop_store')['wxapp']['wxapp_id'];

        db('wxapp')->where(['wxapp_id'=>$wxapp_id])->update(['dealer_case'=>$data['dealerCase']]);

        return $this->allowField(true)
            ->insertGetId([
                'wxapp_id'=>$wxapp_id,
                'setting_content'=>$setting_content,
                'create_time'=>time(),
                'update_time'=>time(),
                'dealer_type'=>$dealer_type
            ]);
    }
    /*
     * 修改分销方案二配置
     */
    public function edittow($id,$data){

        $newData = [
            'common' => $data['common'],
            'staff' => $data['staff'],
            'wgd' => $data['wgd'],
            'vip' => $data['vip'],
        ];
        $setting_content = json_encode($newData,256);

        $wxapp_id = Session::get('yoshop_store')['wxapp']['wxapp_id'];


        db('wxapp')->where(['wxapp_id'=>$wxapp_id])->update(['dealer_case'=>$data['dealerCase']]);

        return $this->allowField(true)->where(['id'=>$id])->update([
            'wxapp_id'=>$wxapp_id,
            'setting_content'=>$setting_content,
            'update_time'=>time(),
        ]);

    }

}